//arg: url,img,loading,divtext,func;
function ajaxLoader()
{
    this.http = null;
    this.err  = null;
    this.posthas=new Array;
    this.caches=new Array;
    
    this.init = function(arg)
    {
        try {
            this.http = new XMLHttpRequest()
        }
        catch (e)
        {
            try {
                this.http = new ActiveXObject("Msxml2.XMLHTTP")
            }
            catch (e) {
                this.http = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if (this.http == null) {
            alert("Browser does not support HTTP Request");
            this.err=1;
        }
        this.arg=arg;
    };
    this.addPost    = function(name,vl)
    {
        this.posthas[name]=vl;
    };
    this.onchange   = function()
    {
        if (this.http.readyState == 1)
        {
            try
            {
				if(this.arg.img==undefined)
                {
                    this.arg.img="ajaxldf";   
                }
                this.ld=document.createElement("span");
                this.ld.innerHTML='<span class="img"></span>';
                this.ld.className=this.arg.img;
                this.dld=document.getElementById(this.arg.loading);
                this.dld.insertBefore(this.ld,this.dld.firstChild);
            }
            catch (e) {}
        }
        if (this.http.readyState == 4)
        {
            try
            {
                this.dld.removeChild(this.ld);
            }
            catch (e) {}
            if(this.http.status != 200)
            {
                alert("There was a problem with the request.");
                return;
            }
            this.success(this.http.responseText);  
            this.caches[this.upc]=this.http.responseText;
        }
    };
    this.success = function(datares)
    {
        this.sending=false;          
        if(this.arg.debug==true)
        {
        	alert(datares);
        }
        try {
            document.getElementById(this.arg.loading).innerHTML = '';
        }
        catch (e) {}
        try{
			eval("objson="+datares);	
			if(objson.act=="Redirect")
			{
				location=objson.url;
				return;
			}		
			document.getElementById(this.arg.divtext).innerHTML = objson.msg;		
		}catch(e){}            
        try {
        	if(typeof(objson)=="object")
			{
				objson.f=this.arg.f;
				this.arg.func(objson);
				return;
			}	
            
            this.arg.func(datares);
        }
        catch (e) {}
        try {
            document.getElementById(this.arg.divtext).innerHTML = datares;
        }
        catch (e) {}     
    };
    this.send = function(post)
    {
        var _root=this;        
        if(this.sending==true)return;        
        this.sending=true;
        if (post == undefined) {
            this.http.open("GET", this.arg.url, true);
            post="";
        }
        else
        {
            this.http.open("POST", this.arg.url, true);
            this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            post+="&ajax=true&";            
            for(i in this.posthas)
            {
                post+='&'+i+'='+this.posthas[i];
            }
        }
        var upc=post.replace(new RegExp("&|=","g"),'');
        this.upc=upc;
        if(this.arg.hascache==true && this.caches[upc])
        {            
            this.success(this.caches[upc]);
            return;
        }
        this.http.onreadystatechange = function()
        {
            _root.onchange();
        };
        this.http.send(post);
    };  
    this.escapeHTML = function(str)
    {
      str   = escape(str);
      str   = str.replace(/\+/g,'%2B');
      return str;
    };  
}